//package com.fbs.service.impl;
//
//import com.fbs.dao.BookStockDao;
//import com.fbs.entity.BookStock;
//import com.fbs.service.BookStockService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.data.redis.core.ValueOperations;
//import org.springframework.stereotype.Service;
//
//import java.util.concurrent.TimeUnit;
//
//@Service
//public class BookStockServiceImpl03 implements BookStockService {
//    @Autowired
//    private BookStockDao bookStockDao;
//    @Autowired
//    private StringRedisTemplate redisTemplate;
//    @Override
//    public String buyByIsbn(String isbn) {
//        ValueOperations<String, String> stringStringValueOperations = redisTemplate.opsForValue();
//        Boolean flog = stringStringValueOperations.setIfAbsent("isbn::" + isbn, "fbs", 30, TimeUnit.SECONDS);
//        //synchronized (BookStockServiceImpl.class){
//        if (flog){
//            try {
//
//
//        BookStock bookStock = bookStockDao.selectById(isbn);
//        if (bookStock.getStock() > 0){
//            bookStock.setStock(bookStock.getStock()-1);
//            bookStockDao.updateById(bookStock);
//            System.out.println("书的isbn编号为"+isbn+"商品剩余"+bookStock.getStock()+"本");
//            return "书的isbn编号为"+isbn+"书本剩余"+bookStock.getStock()+"本";
//        }else {
//            System.out.println("isbn编号为"+isbn+"库存不足");
//        return "isbn编号为"+isbn+"库存不足";
//        }
//            }finally {
//                redisTemplate.delete("isbn::"+isbn);
//            }
//        }else {
//            System.out.println("服务器正忙");
//            return "服务器正忙";
//        }
//   // }
//    }
//}
